import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";

/**
 * 高亮组件
 */
export default class HighlighterComponent {
    private static instance: HighlighterComponent = new HighlighterComponent();
    /**
     * 高亮组件
     */
    public static get Default(): HighlighterComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    public async UpdateAsync(instances: Array<number>): Promise<boolean> {
        const response: ClientMessage.UpdateHighlighterResponse = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.UpdateHighlighterRequest(instances));
        return response.Error == 0;
    }

    public async ClearAsync(): Promise<boolean> {
        const response: ClientMessage.UpdateHighlighterResponse = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.UpdateHighlighterRequest());
        return response.Error == 0;
    }
}